package com.gongpingjialoan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.AssessHistory;
import com.gongpingjialoan.imageload.ImageLoad;

import java.util.List;

/**
 * Created by Administrator on 2016/5/13.
 */
public class EvalAdapter extends BaseAdapter {
    private List<AssessHistory> mAssessHistories;

    private Context mContext;

    private LayoutInflater mLayoutInflater;

    public EvalAdapter(List<AssessHistory> assessHistories, Context context) {
        mAssessHistories = assessHistories;
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public void setData(List<AssessHistory> mAssessHistories){
        this.mAssessHistories = mAssessHistories;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mAssessHistories.size();
    }

    @Override
    public Object getItem(int position) {
        return mAssessHistories.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.eval_item, null);
            holder = new ViewHolder();
            holder.imgThumbnail = (ImageView) convertView.findViewById(R.id.pic);
            holder.tvCarTitle = (TextView) convertView.findViewById(R.id.carName);
            holder.timeTextView = (TextView) convertView.findViewById(R.id.time);
            holder.mileTextView = (TextView) convertView.findViewById(R.id.mile);
            holder.priceTextView = (TextView) convertView.findViewById(R.id.price);

            holder.reviewTextView = (TextView) convertView.findViewById(R.id.review);
            holder.hangqingView = convertView.findViewById(R.id.linear2);
            holder.hangqingImageView = (ImageView) convertView.findViewById(R.id.hangqing);
            convertView.setTag(R.id.tag_first, holder);
        } else {
            holder = (ViewHolder) convertView.getTag(R.id.tag_first);
        }

        AssessHistory source = mAssessHistories.get(position);
        convertView.setTag(R.id.tag_second, source);

        if(Float.parseFloat(source.eval_price_change) > 0 ){
            holder.hangqingImageView.setImageResource(R.drawable.zhang);
            holder.hangqingView.setVisibility(View.VISIBLE);
        }else if(Float.parseFloat(source.eval_price_change) < 0 ){
            holder.hangqingImageView.setImageResource(R.drawable.jiang);
            holder.hangqingView.setVisibility(View.VISIBLE);
        }else {
            holder.hangqingView.setVisibility(View.GONE);
        }

        if("false".equals(source.review)){
            holder.reviewTextView.setVisibility(View.GONE);
        }else{
            holder.reviewTextView.setVisibility(View.GONE);
        }
//        holder.domainTextView.setText(source.d);
        holder.tvCarTitle.setText(source.model + source.model_detail);
        holder.mileTextView.setText(source.year + "年上牌/" + source.mile + "万公里");
        holder.timeTextView.setText(source.create_time);

            holder.priceTextView.setText(source.eval_price);
        ImageLoad.LoadImage( holder.imgThumbnail, source.url, R.drawable.car_loading, R.drawable.car_no,mContext);
        return convertView;
    }

    public static class ViewHolder {
        public ImageView imgThumbnail;

        public TextView tvCarTitle;

        public TextView timeTextView;

        public TextView mileTextView;

        public TextView priceTextView;

        public TextView domainTextView;

        public  TextView reviewTextView;

        public View hangqingView;

        public ImageView hangqingImageView;

    }
}
